package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.common.UserMemberEditResult;
import com.yh.homestay.dao.UserMemberLikeMapper;
import com.yh.homestay.dao.UserMemberMapper;
import com.yh.homestay.pojo.UserMember;
import com.yh.homestay.pojo.UserMemberExample;
import com.yh.homestay.service.UserMemberService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 */

@Service
public class UserMemberServiceImpl implements UserMemberService {
    @Autowired
    private UserMemberMapper userMemberMapper;
    @Autowired
    private UserMemberLikeMapper userMemberLikeMapper;


    @Override
    public HomeStayResult selectAll(Integer page, Integer limit, String userName) {
        Map map=new HashMap();
        map.put("userName",userName);
        PageHelper.startPage(page,limit);
        List<UserMember> byName = userMemberLikeMapper.findByName(map);
        PageInfo pageInfo = new PageInfo(byName);
        HomeStayResult res=new HomeStayResult(0," ", (int) pageInfo.getTotal(),pageInfo.getList());
        return res;
    }

    @Override
    public HomeStayResult addUser(UserMember userMember) {
        int insert = userMemberMapper.insert(userMember);
        if(insert>0){
            return new HomeStayResult(200, "ok",0,null);
        }else{
            return new HomeStayResult(500, "error",0,null);
        }

    }

    @Override
    public HomeStayResult delete(Integer id) {
        UserMember userMember = new UserMember();
        userMember.setUserId(id);
        int delete = userMemberMapper.delete(userMember);
        if (delete > 0) {
            return new HomeStayResult(200, "ok",0,null);

        } else {
            return new HomeStayResult(500, "error",0,null);
        }

    }

    @Override
    public HomeStayResult update(Integer id, String userName, String userNickname, String userPhone, Integer userIntegral) {
        UserMember userMember = new UserMember();
        // userMember.setUserId(id);
        userMember.setUserName(userName);
        userMember.setUserNickname(userNickname);
        userMember.setUserPhone(userPhone);
        userMember.setUserIntegral(userIntegral);
        UserMemberExample example = new UserMemberExample();
        UserMemberExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        int update = userMemberMapper.updateByExampleSelective(userMember, example);
        if (update > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }


    }


    @Override
    public UserMemberEditResult selectone(Integer id) {
        UserMemberExample userMemberExample = new UserMemberExample();
        UserMemberExample.Criteria criteria = userMemberExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        List<UserMember> userMembers = userMemberMapper.selectByExample(userMemberExample);
//       userMemberMapper.updateByPrimaryKeySelective()
        if (userMembers != null && userMembers.size() > 0) {
            return new UserMemberEditResult(200, "ok", userMembers.get(0));
        } else {
            return new UserMemberEditResult(500, "err", null);

        }
    }

    @Override
    public UserMemberEditResult selecteUserById(Integer id) {
        UserMember user = userMemberMapper.selectByPrimaryKey(id);

        return new UserMemberEditResult(200, "ok", user);
    }

    @Override
    public UserMemberEditResult updateUserMember(UserMember userMember) {
        userMemberMapper.updateByPrimaryKeySelective(userMember);
      return new UserMemberEditResult(200, "ok", null);

    }

    /**
     *批量删除
     * @param ids
     * @return
     */
    @Override
    public HomeStayResult deleteUserS(List<Integer> ids) {
        UserMemberExample example = new UserMemberExample();
        UserMemberExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdIn(ids);
        int delete = userMemberMapper.deleteByExample(example);
        if (delete > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
        }


}